#ifndef _KEY_HPP
#define _KEY_HPP


// 电键状态
static bool keyStatus_1 = false;
static bool keyStatus_2 = false;

static TaskHandle_t keyCheckHandle;
static TaskHandle_t keyOpH;

static uint64_t keyPressTime = 0;
static uint64_t keyIdleStart = 0;
static bool keyInputCheck = false;


void keyInitTask();
void keyOpTask();
static void key_1();
static void key_2();
static void manualKey(bool status);
static void keyCheckStart();
static void keyCheckStop();
static void keyAddInputChar(char *pStr);
static void keyInputCheckTask();

static void keyInputCheckTask() {
	TickType_t keyCheckTick = xTaskGetTickCount();
	vTaskDelayUntil(&keyCheckTick, stDiTime * 1.5 / 10);
	keyAddInputChar(" ");
	vTaskDelayUntil(&keyCheckTick, stDiTime * 2.5 / 10);
	keyAddInputChar(" ");
	vTaskDelayUntil(&keyCheckTick, (stAutoSendTime -  (stDiTime * 4)) / 10);
	xTaskNotify(wsSendOpH, BIT0, eSetValueWithoutOverwrite);
	keyInputCheck = false;
	vTaskDelete(NULL);
}


static void keyCheckStart() {
	if(!keyInputCheck) {
		keyInputCheck = true;
		xTaskCreate(keyInputCheckTask, "keyInputCheckTask", 1024 * 3, NULL, 0, &keyCheckHandle);
	}
}

static void keyCheckStop() {
	if(keyInputCheck) {
		keyInputCheck = false;
		vTaskDelete(keyCheckHandle);
	}
}


void keyInitTask() {
	gpio_config_t keyConfig_1 =
	{
		.intr_type = 0,
		.mode = GPIO_MODE_INPUT,
		.pin_bit_mask = (1ULL << CONFIG_PIN_KEY_1),
		.pull_up_en = 1
	};
	gpio_config(&keyConfig_1);

	gpio_config_t keyConfig_2 = 
	{
		.intr_type = 0,
		.mode = GPIO_MODE_INPUT,
		.pin_bit_mask = (1ULL << CONFIG_PIN_KEY_2),
		.pull_up_en = 1
	};
	gpio_config(&keyConfig_2);

	// 电键操作
	xTaskCreatePinnedToCore(keyOpTask, "keyOpTask", 1024 * 3, NULL, 0, &keyOpH, 1);

	while(1)
	{
		if(!statusCheck(STATUS_PLAY_CODE)) {
			if(gpio_get_level(CONFIG_PIN_KEY_1) == 0 && !keyStatus_1)
			{
				keyStatus_1 = true;
				xTaskNotify(keyOpH, BIT0, eSetValueWithoutOverwrite);
			}
			if(gpio_get_level(CONFIG_PIN_KEY_1) == 1 && keyStatus_1)
			{
				keyStatus_1 = false;
				xTaskNotify(keyOpH, BIT0, eSetValueWithoutOverwrite);
			}
			if(gpio_get_level(CONFIG_PIN_KEY_2) == 0 && !keyStatus_2)
			{
				keyStatus_2 = true;
				xTaskNotify(keyOpH, BIT1, eSetValueWithoutOverwrite);
			}
			if(gpio_get_level(CONFIG_PIN_KEY_2) == 1 && keyStatus_2)
			{
				keyStatus_2 = false;
				xTaskNotify(keyOpH, BIT1, eSetValueWithoutOverwrite);
			}
		}
		vTaskDelay(pdMS_TO_TICKS(30));
	}
	vTaskDelete(NULL);
}

void keyOpTask() {
	uint32_t bit;
	while(1) {
		xTaskNotifyWait(
			0x00,
			ULONG_MAX,
			&bit,
			portMAX_DELAY
		);
		if((bit & BIT0) != 0) {
			key_1();
		}
		if((bit & BIT1) != 0) {
			key_2();
		}
	}
}

static void key_1() {
	// 手动建 按下
	if(stKeyType & !stKeySwap) {
		manualKey(keyStatus_1);
	}
}

static void key_2() {
	printf("key 2 do\n");
}

// 手动建
static void manualKey(bool status) {
	// 手动键按下
	if(status) {
		beeStart();
		keyCheckStop();
		keyPressTime = getTimeUs();
	}
	// 手动键释放
	else {
		beeStop();
		if(getTimeUs() - keyPressTime < stDiTime * 1000) {
			keyAddInputChar(".");
		}
		else {
			keyAddInputChar("-");
		}
		keyPressTime = 0;
		keyIdleStart = getTimeUs();
		keyCheckStart();
	}
}

static void keyAddInputChar(char *pStr) {
	strcat(inputCode, pStr);
	xQueueSend(inputQ, &pStr, 0);
}



#endif